//
//  AllColumnViewController.m
//  礼物go
//
//  Created by dllo on 16/5/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AllColumnViewController.h"
#import "AllColumnTableViewCell.h"
#import "TopicModel.h"

#import "CycleColumnViewController.h"
static NSString *const allcolumnCell = @"allColumnCell";

@interface AllColumnViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)UITableView *AllColumnTableView;
@property(nonatomic,assign)NSInteger nextPage;

@end

@implementation AllColumnViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"全部专题";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Arial" size:20],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"<返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.translucent = NO;
    
    self.dataArray = [NSMutableArray array];
    
    self.AllColumnTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, self.view.frame.size.height - 66) style:UITableViewStylePlain];
    [self.view addSubview:self.AllColumnTableView];
    self.AllColumnTableView.dataSource = self;
    self.AllColumnTableView.delegate = self;
    [self.AllColumnTableView registerClass:[AllColumnTableViewCell class] forCellReuseIdentifier:allcolumnCell];
    self.AllColumnTableView.showsVerticalScrollIndicator = NO;
    
    self.nextPage = 0;
    [self getSelectList:self.nextPage];
    [self addFooter];
    
    
    
    // Do any additional setup after loading the view.
}

-(void)addFooter{
    __block AllColumnViewController *allCVC = self;
    // 添加上拉刷新尾部控件
    self.AllColumnTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        allCVC.nextPage ++;
        [allCVC getSelectList:allCVC.nextPage];//请求数据
    }];
}

-(void)getSelectList:(NSUInteger)nextCursor{
    AFNetworkReachabilityManager *netWorkManger = [AFNetworkReachabilityManager sharedManager];
    NSString *urlStr = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/collections?limit=20&offset=%ld",20 * nextCursor];
    [LYNetworking ly_GetWithURL:urlStr Parameter:nil success:^(id result) {
        [netWorkManger stopMonitoring];
        NSDictionary *dataDic = [result objectForKey:@"data"];
        NSArray *collArray = [dataDic objectForKey:@"collections"];
        for (NSDictionary *dic in collArray) {
            TopicModel *topicModel = [[TopicModel alloc] init];
            [topicModel setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:topicModel];
            //NSLog(@"%@",topicModel.title);
        }
        [self.AllColumnTableView.mj_footer endRefreshing];
        
        [self.AllColumnTableView reloadData];
        
        
    } error:^(NSError *error) {
        
        
    }];

    

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   AllColumnTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:allcolumnCell];
    TopicModel *topic = [self.dataArray objectAtIndex:indexPath.row];
    cell.topicModel = topic;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
  
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TopicModel *topic = [self.dataArray objectAtIndex:indexPath.row];
    NSString *data = [NSString stringWithFormat:@"%@",topic.myId];
    CycleColumnViewController *cycVC = [[CycleColumnViewController alloc] init];
    cycVC.dataId = data;
    [self.navigationController pushViewController:cycVC animated:YES];

}




-(void)leftButtonAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
